<?php

class ControllerSaleOrderBasket extends AdminController {

	public function index() {
		M('sale/order_basket');
		$this->getList();
	}

  	protected function getList() {
  		$this->document->setTitle($this->_['heading_title']);
  		$qf = new Query_filter();
  		
  		$this->data['filter_basket_id'] = $qf->get('filter_basket_id');
  		$this->data['filter_order_id'] = $qf->get('filter_order_id');
  		
  		$this->data['baskets'] = $this->model_sale_order_basket->getBaskets($qf, $total);
  		
  		$this->data['pagination'] = $this->pagination($total)->render();
  		
  		$this->display('sale/order_basket_list.tpl');
  		
  	}
  	
  	public function smartedit() {
  		if ($this->hasMP()) {
  			$order_basket_id = (int) G('id');
  			$field = G('field');
  			$value = G('val');
  			$rs = $this->db->update('order_basket', array($field => $value), "order_basket_id=$order_basket_id");
  		}
  		else $rs = 0;
  		
  		$this->response->setOutput(json_encode(array('status' => $rs)));
  	}
  	
	public function insert() {

		if ($this->request->isPost()) {
			
			$qf = new Query_filter();
			$qf->set('filter_order_status_id', P('filter_order_status_id'));
			$qf->set('filter_date_start', P('filter_date_start'));
			$qf->set('filter_date_end', P('filter_date_end'));
			M('sale/order_basket');
			$this->model_sale_order_basket->addOrderBasket($qf);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/order_basket', 'filter_reset=1'));
		}

		$this->document->setTitle($this->_['heading_title']);
		$this->document->addStyle('view/stylesheet/jquery.minicolors.css');
		
		M('localisation/order_status');
		$order_statuses = array();
		$rs = $this->model_localisation_order_status->getOrderStatuses();
		foreach ($rs as $row) {
			$order_statuses[$row['order_status_id']] = $row;
		}
		$this->data['order_statuses'] = $order_statuses;
		
		$qf = new Query_filter();
		
		$this->data['filter_date_start'] = $qf->get('filter_date_start');
		$this->data['filter_date_end'] = $qf->get('filter_date_end');
		$this->data['filter_order_status_id'] = $qf->get('filter_order_status_id');

		$qf->set('order_basket_id', 0);

		M('sale/order');

		$order_total = $this->model_sale_order->getTotalOrders($qf);

		$this->data['orders'] = $this->model_sale_order->getOrders($qf);

		foreach ($this->data['orders'] as $i => $order) {
			$this->data['orders'][$i]['status'] = isset($order_statuses[$order['order_status_id']]) ? $order_statuses[$order['order_status_id']] : '';
			$this->data['orders'][$i]['total']  = $this->currency->format($order['total'], $order['currency_code'], $order['currency_value']);
			$this->data['orders'][$i]['date_added'] = date('d/m/Y H:i', strtotime($order['date_added']));
			$this->data['orders'][$i]['date_modified'] = date('d/m/Y H:i', strtotime($order['date_modified']));
		}

		$url = UA('sale/order_basket/insert', 'page={page}');
		$page = $qf->get('page', 1);
		$this->data['pagination'] = $this->pagination($order_total, $url)->render();

		$this->display('sale/order_basket_insert.tpl');
	}

	public function delete() {
		
		if (isset($this->request->post['selected'])) {
			M('sale/order_basket');
			foreach ($this->request->post['selected'] as $order_basket_id) {
				$this->model_sale_order_basket->deleteOrderBasket($order_basket_id);
			}
			$this->session->set_flashdata('success', $this->_['text_success']);
		}
		$this->redirect(UA('sale/order_basket', 'filter_reset=1'));
	}
	
	public function orders() {
		$this->document->setTitle($this->_['heading_title']);
		$this->document->addStyle('view/stylesheet/jquery.minicolors.css');
		
		$order_basket_id = $this->request->get('order_basket_id');

		if ($this->request->isPost() && $this->hasMP()) {
			M('sale/order_basket');
			$order_ids = P('selected');
			if (!empty($order_ids)) {
				$this->model_sale_order_basket->deleteOrder($order_basket_id, $order_ids);
			}
			$order_id = P('order_id');
			if ($order_id) {
				$this->model_sale_order_basket->addOrder($order_basket_id, $order_id);
			}
			$this->data['success'] = $this->_['text_success'];
		}


		//order status
		M('localisation/order_status');
		$order_statuses = array();
		$rs = $this->model_localisation_order_status->getOrderStatuses();
		foreach ($rs as $row) {
			$order_statuses[$row['order_status_id']] = $row;
		}
		
		$this->data['order_statuses'] = $order_statuses;

		$qf = new Query_filter('order_basket_query_filter');

		M('sale/order');
		$order_total = $this->model_sale_order->getTotalOrders($qf);

		$this->data['orders'] = $this->model_sale_order->getOrders($qf);
		
		foreach ($this->data['orders'] as $i => $order) {
			$action = array();

			$action[] = array(
				'text' => $this->_['text_view'],
				'href' => UA('sale/order/info', 'order_id=' . $order['order_id'])
			);

			if (strtotime($order['date_added']) > strtotime('-' . (int)C('config_order_edit') . ' day')) {
				$action[] = array(
					'text' => $this->_['text_edit'],
					'href' => UA('sale/order/update', 'order_id=' . $order['order_id'])
				);
			}
			
			$action[] = array(
				'text' => $this->_['text_print_invoice'],
				'href' => UA('sale/order/invoice', 'order_id=' . $order['order_id'])
			);
			
			$action[] = array(
					'text' => $this->_['text_shipping_label'],
					'href' => UA('sale/order/shippinglabel', 'order_id=' . $order['order_id'])
			);
			
			$this->data['orders'][$i]['status'] = isset($order_statuses[$order['order_status_id']]) ? $order_statuses[$order['order_status_id']] : '';
			$this->data['orders'][$i]['total']  = $this->currency->format($order['total'], $order['currency_code'], $order['currency_value']);
			$this->data['orders'][$i]['date_added'] = date('d/m/Y H:i', strtotime($order['date_added']));
			$this->data['orders'][$i]['date_modified'] = date('d/m/Y H:i', strtotime($order['date_modified']));
			$this->data['orders'][$i]['action'] = $action;
		}

		$url = UA('sale/order_basket/orders', "order_basket_id={$order_basket_id}&page={page}");
		$this->data['pagination'] = $this->pagination($order_total, $url)->render();

		$this->data['order_basket_id'] = $order_basket_id;
		$this->data['filter_customer'] = $qf->get('filter_customer');
		$this->data['filter_model'] = $qf->get('filter_model');
		$this->data['filter_date_start'] = $qf->get('filter_date_start');
		$this->data['filter_order_id'] = $qf->get('filter_order_id');
		$this->data['filter_date_end'] = $qf->get('filter_date_end');
		$this->data['filter_order_status_id'] = $qf->get('filter_order_status_id');
		
		$this->display('sale/order_basket_orders.tpl');
	}

	public function skus() {

		$order_basket_id = $this->request->get('order_basket_id');

		M('catalog/supplier');
		$suppliers = $this->model_catalog_supplier->getSupplierOptions();

		$this->data['skus'] = array();

		M('tool/image');

		M('sale/order_basket');
		$this->data['order_basket'] = $this->model_sale_order_basket->getOrderBasket($order_basket_id);
		
		$results = $this->model_sale_order_basket->getSKUs($order_basket_id);

		foreach($results as $r) {
			if ($r['image'] && file_exists(DIR_IMAGE . $r['image'])) {
				$r['image'] = $this->model_tool_image->resize($r['image'], 100, 100);
			} else {
				$r['image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
			}
			if ($r['supplier_id'] && isset($suppliers[$r['supplier_id']])) {
				$r['supplier'] = $suppliers[$r['supplier_id']];
			}
			else {
				$r['supplier'] = '';
			}
			$this->data['skus'][] = $r;
		}

		$this->display('sale/order_basket_skus.tpl', false);
	}
}